package com.kurumi.autoprint.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.autoprint.enums.ApiStatusEnum;
import com.kurumi.autoprint.pojo.MrPrintType;
import com.kurumi.autoprint.service.MedRecordService;
import com.kurumi.autoprint.util.ExceptionUtil;
import com.kurumi.autoprint.util.FileUtil;
import com.kurumi.autoprint.util.IpUtil;
import com.kurumi.autoprint.util.Result;

@Controller
public class IndexController {
	private Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Value("${interface.toIndexPage}")
	private String interfaceToIndexPage;
	
	@Autowired
	private MedRecordService medRecordService;
	
	
	/**
	 * 跳转到打印首页
	 * @return
	 */
	@GetMapping("/printIndex")
	public String index(HttpServletRequest request, Model model) {
		try {
			logger.info("获取客户端ip地址:" + IpUtil.getIpAddr(request));
			List<MrPrintType> mrPrintTypeListNew = new ArrayList<MrPrintType>();
					
			if (interfaceToIndexPage.equals(ApiStatusEnum.PRODUCE.getValue())) {
				List<MrPrintType> mrPrintTypeList = medRecordService.getPrintTypes();
				if (CollectionUtils.isNotEmpty(mrPrintTypeList)) {
					for (MrPrintType mpt : mrPrintTypeList) {
						if (mpt.getCode() != null && mpt.getCode().intValue() == 33) {
							mpt.setName("常规打印（不含检验）");
						}
						
						if (mpt.getCode() != null && mpt.getCode().intValue() == 34) {
							mpt.setName("常规打印（含检验）");
							MrPrintType mrPrintTypeNew = new MrPrintType();
							mrPrintTypeNew.setCode(34);
							mrPrintTypeNew.setName(mpt.getName());
						}
					}
				}
				
				model.addAttribute("mrPrintTypeList", mrPrintTypeListNew );
			}
		} catch (Exception e) {
			logger.error("查询打印类型失败!" + ExceptionUtil.getStackTrace(e));
		}
		
		return "/printIndex";
	}
	
	@GetMapping("/ipjson")
	@ResponseBody
	public Result ipjson() {
		return new Result(true, "", FileUtil.readResourceFile("payUmsTidBindIp.json "),200);
	}
	
	
}
